global.config = "dll";
const os = require('os');
const path = require('path');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const SimpleProgress = require('simple-progress-webpack-plugin');
const IsDevEnv = process.env.NODE_ENV !== 'production';
const parallel = os.cpus().length - 1;
// 根据环境变量，控制编译出那个“common”
const GetEntry = (() => {
    let entry = {};
    entry["common"] = [
        "phaser",
    ];
    return entry;
})();

const setPlugins = function() {
    let options = [
        new webpack.HashedModuleIdsPlugin({
            hashDigestLength: 8
        }),

        new webpack.DllPlugin({
            context: path.resolve(__dirname, "dist", "js"),
            path: path.join(__dirname, "[name]-manifest.json"),
            name: "[name]_[hash:8]"
        }),

        new SimpleProgress({
            format: 'compact'
        })
    ];
    return options;
}

module.exports = {
    // 生成 source map
    devtool: "cheap-module-source-map",

    // 模式
    mode: IsDevEnv ? "development" : "production",

    // 性能
    performance: {
        // 枚举
        hints: "warning",
        // 整数类型（以字节为单位）,400Kb
        maxAssetSize: 400000,
        // 整数类型（以字节为单位）,400Kb
        maxEntrypointSize: 400000,
        // 过滤检测文件
        assetFilter: function(assetFilename) {
            // 提供资源文件名的断言函数
            return /\.(css|js)$/.test(assetFilename);
        }
    },

    // 优化
    optimization: {
        // 在编译时出现错误时，使用跳过发射阶段
        noEmitOnErrors: true,
        // 压缩 js 文件
        minimizer: [
            new TerserPlugin({
                terserOptions: {
                    compress: {
                        drop_console: true
                    },
                    output: {
                        comments: false,
                        safari10: true
                    },
                    mangle: true
                },
                sourceMap: true,
                cache: true,
                parallel: parallel
            })
        ]
    },

    // 入口
    entry: GetEntry,

    // 出口
    output: {
        path: path.resolve(__dirname, "dist"),
        publicPath: "//project.com/dist/",
        filename: "js/[name].js",
        library: "[name]_[hash:8]",
        libraryTarget: "var"
    },

    // 解析器
    resolve: {
        extensions: [".js", "jsx", "*"],
        alias: {
        }
    },

    // 模块
    module: {
        rules: [{
            test: /\.(js|jsx)$/,
            exclude: /node_modules|plugins/,
            use: "babel-loader"
        }],
        noParse: [/sjcl\.js$/]
    },
    // 插件
    plugins: setPlugins(),

    // 统计信息
    stats: {
        // 添加构建模块信息
        modules: false
    }
};